package com.vgemv.jsutilitysdk.Request.JsRequestManager2;


import android.content.Context;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

import com.google.gson.Gson;
import com.vgemv.jsutilitysdk.JsJsonManager.JsJsonManager;
import com.vgemv.jsutilitysdk.Request.JsRequestInterface;
import com.vgemv.jsutilitysdk.JsThread.JsThreadManager;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestBaseData.IJsResponseHandler;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestConfig;
import com.vgemv.jsutilitysdk.Request.JsRequestManager2.JsRequestExecutor.JsRequestExecutorOh;
import com.vgemv.jsutilitysdk.SharedPreference.JsSharedPreferencesManager;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * JsRequestManager.
 * Funciton:
 *  Network reqeust.
 *
 * Dependency:
 *  Gson
 *  JsSharedPreferencesManager
 *
 *  Cookie的管理,由Manager来完成.
 *  Executor只负责执行接口调用.
 *
 *
 */
public class JsRequestManager2  {

    final static String cookiesKey = "cookiesKey";
    public String serverAddress = null;

    private static JsRequestManager2 _sharedInstance;


    private OkHttpClient mDefaultClient;



    private Context mContext;

    private JsRequestExecutorOh mExecutor;

     private JsCookieManager mCookieManager;

    public boolean mb_isSaveCookieToSp = true;

    public static JsRequestManager2 sharedInstance() {
        return _sharedInstance;
    }

    public static void initClass(Context context,boolean isSaveCookie) {
        _sharedInstance = new JsRequestManager2(context,isSaveCookie);
    }

    /**
     * 通用得调用方法.
     * 职责:
     * 1,补全config的serverAddress.
     * @param config
     * @param handler
     */
    public void invokeRequest(JsRequestConfig config, IJsResponseHandler handler) {
        if(config.serverAddress == null) {
            config.serverAddress = serverAddress;
        }
        mExecutor.invokeRequest(config,handler);
    }


    public JsRequestManager2(Context context, boolean isSaveCookie) {


        mContext = context;
        mCookieManager = new JsCookieManager(context);
        mExecutor =  new JsRequestExecutorOh();
        mb_isSaveCookieToSp = true;
        mExecutor.mCookieOpCallback = new JsCookieOpCallback() {
            @Override
            public List onGetCookie(HttpUrl httpUrl) {
                //请求获取cookie
                return mCookieManager.getCookies(httpUrl);
            }

            @Override
            public void onSetCookie(HttpUrl httpUrl, List cookies) {
                //请求保存cookie
                mCookieManager.addCookies(httpUrl,cookies);

            }
        };

    }

    public void clearAllCookies() {

    }

}